
package com.be.beadmin.project.impl;

import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.project.dto.input.ChangeDto;
import com.be.beadmin.project.dto.input.ContractDto;
import com.be.beadmin.project.dto.output.ChangeEx;
import com.be.beadmin.project.entity.Change;
import com.be.beadmin.project.entity.Change_;
import com.be.beadmin.project.entity.Info;
import com.be.beadmin.project.entity.MasterPlan;
import com.be.beadmin.project.mapper.ChangeMapper;
import com.be.beadmin.project.mapper.ChangeMapperEx;
import com.be.beadmin.project.repository.ChangeRepository;
import com.be.beadmin.project.repository.InfoRepository;
import com.be.beadmin.project.repository.MasterPlanRepository;
import com.be.beadmin.project.service.ChangeService;
import com.be.beadmin.service.dto.output.DictDataEx;
import com.be.beadmin.service.entity.DictType;
import com.be.beadmin.service.repository.DictTypeRepository;
import com.be.beadmin.service.service.DictDataService;
import com.be.beadmin.service.service.SerialNumberService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description 服务实现
* @author ljh
* @date 2023-02-05
**/
@Service
@RequiredArgsConstructor
public class ChangeServiceImpl implements ChangeService {

    @Autowired
    private  ChangeRepository changeRepository;
    @Autowired
    private  ChangeMapper changeMapper;
    @Autowired
    private  ChangeMapperEx changeExMapper;

    @Autowired
    private InfoRepository infoRepository;

    @Autowired
    private MasterPlanRepository masterPlanRepository;

    @Autowired
    private DictTypeRepository dictTypeRepository;

    @Autowired
    private DictDataService dictDataService;

    @Autowired
    private SerialNumberService serialNumberService;

    /**
     * 获取变更类型
     * @return
     */
    @Override
    public List<String> getChangeType() {
        DictType dictType = dictTypeRepository.findDictTypeByDictName("变更类型");

        List<DictDataEx> changeType =  dictDataService.getDictDataExByDictTypeId(dictType);
        List<String> changeTypeList = new ArrayList<>();
        changeType.forEach(role-> changeTypeList.add(role.getKeyName()));
        return changeTypeList;
    }

    /**
     * 获取合同编码和名称
     * @return
     */
    @Override
    public List<ContractDto> getContractCodeAndName() {
        List<Info> infoList = infoRepository.findAll();
        List<ContractDto> contractList = new ArrayList<>();
        for (int i = 0; i < infoList.size(); i++) {
            ContractDto contractDto = new ContractDto();
            contractDto.setContractCode(infoList.get(i).getContractCode());
            contractDto.setContractName(infoList.get(i).getContractName());
            if (infoList.get(i).getDelete()==0){
                contractList.add(contractDto);
            }
        }
        return contractList;
    }

    @Override
    public Page<ChangeEx> getChangeByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<Change> page = changeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(Change_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(Change_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<ChangeEx> page1 = page.map(changeExMapper::toEntity);
            return page1;

    }

    @Override
    public void addChange(ChangeDto changeDto) {
            //默认柜子相关字段前端传递

            String changeCode = serialNumberService.getNextSnByCode("ChangeCode");
            changeDto.setChangeCode(changeCode);

            Change change = changeMapper.toEntity(changeDto);
            changeRepository.save(change);
    }

    @Override
    public void updateChange(ChangeDto changeDto) {
            Change change = changeRepository.findChangeById(changeDto.getId());
            change = changeMapper.toEntity(changeDto);
            changeRepository.save(change);
    }


    @Override
    public void delChange(String id) {
        Change change = changeRepository.findChangeById(id);
        change.setDelete(1);
        changeRepository.save(change);
    }

    @Override
    public void delChanges (List<String> ids) { }

    @Override
    public ArrayList<Integer> getChangeProgress() {
        Integer changeNumber = changeRepository.countChangeByDelete(0);
        ArrayList<Integer> changeProgress = new ArrayList<>();
        changeProgress.add(0,changeNumber);
        return changeProgress;
    }
}